前端analysis | What,Why,Who,When,Where,How

《Linux》- 用shell向自动部署前进一步

2020-04-21

上传tar包

1
$ scp xxx.tar.gz <user>@xx.xx.xx.xx:/home/data

移动包路径

1
2
3
echo "1.mv tar.gz to data/www"
mv ./$tarDir /data/www
echo -e "mv tar.gz to data/www done \n"

创建新文件夹

1
2
3
4
5
cd /data/www
pwd
echo -e "2.cd at dir data/www\n"

mkdir -p $newDir

移动tar包并解压

1
2
3
4
5
6
7
8
9
10
11
12
echo "3.mv ${tarDir} to ${newDir}"
mv ./$tarDir ./$newDir
echo -e "mv into ${newDir} done\n"

cd ./$newDir
echo -e "4. cd to ${newDir}\n"

echo "5. will tar -xf ${tarDir}"
tar -xf ./$tarDir
echo -e "tar ${tarDir} done \n"

mv ./$tarDir ../

copy healthcheck

1
2
3
4
5
echo "6.will copy healthcheck file"
cp ../$sourceDir/healthcheck.html ./
echo -e "will copy healthcheck file done...\n"
cd ../

修改文件夹权限

1
2
3
echo "7.will chmod ${newDir}"
chmod 755 -R ./$newDir
echo -e "chmod 755 done\n"

文件夹备份、替换

1
2
3
4
5
6
7
echo "8.mv ${sourceDir} to ${bakDir}"
mv $sourceDir $bakDir
echo -e "mv ${sourceDir} to ${bakDir} done \n"

echo "9.mv ${newDir} to ${sourceDir}"
mv $newDir $sourceDir
echo -e "mv ${newDir} to ${sourceDir} done \n"

运行healthcheck

1
2
3
4
5
6
7
8
9
10
11
12
url="http://${healthcheckIp}/healthcheck.html"
echo -e $url
echo -e "\n"

result=$(curl -X GET $url)
echo $result

if [ $result == $okStr ] ; then
echo -e "10.deploy ${tarDir} successfully at ${date}\n"
else
echo -e "deploy ${tarDir} fail at ${date}\n"
fi

最终部署脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash
okStr="OKOKOK"
tarDir=$1
echo $tarDir
echo -e "\n"

sourceDir=${2:-"source"}
echo $sourceDir
echo -e "\n"

HOST_IP=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d '/')
# if 3st argument pass to script null ,use default
healthcheckIp=${3:-$HOST_IP}
echo $healthcheckIp
echo -e "\n"

date=$(date +%Y-%m-%d)
newDir=$(date +%Y-%m-%d-%s)
bakDir="source-build-bak-"$(date +%Y-%m-%d-%s)

# if the param null ; -n is not empty
if [ -z "${tarDir}" ] ; then
echo "tar name is empty will exit deploy "
exit 1
else
echo "will deploy ${tarDir}"
fi

if [ -z "${sourceDir}" ] ; then
echo "sourceDir name is empty will exit deploy "
exit 1
else
echo "will deploy to ${sourceDir}"
fi

echo "1.mv tar.gz to data/www"
mv ./$tarDir /data/www
echo -e "mv tar.gz to data/www done \n"

cd /data/www
pwd
echo -e "2.cd at dir data/www\n"

mkdir -p $newDir

echo "3.mv ${tarDir} to ${newDir}"
mv ./$tarDir ./$newDir
echo -e "mv into ${newDir} done\n"

cd ./$newDir
echo -e "4. cd to ${newDir}\n"

echo "5. will tar -xf ${tarDir}"
tar -xf ./$tarDir
echo -e "tar ${tarDir} done \n"

mv ./$tarDir ../

echo "6.will copy healthcheck file"
cp ../$sourceDir/healthcheck.html ./
echo -e "will copy healthcheck file done...\n"
cd ../

echo "7.will chmod ${newDir}"
chmod 755 -R ./$newDir
echo -e "chmod 755 done\n"

echo "8.mv ${sourceDir} to ${bakDir}"
mv $sourceDir $bakDir
echo -e "mv ${sourceDir} to ${bakDir} done \n"

echo "9.mv ${newDir} to ${sourceDir}"
mv $newDir $sourceDir
echo -e "mv ${newDir} to ${sourceDir} done \n"


url="http://${healthcheckIp}/healthcheck.html"
echo -e $url
echo -e "\n"

result=$(curl -X GET $url)
echo $result

if [ $result == $okStr ] ; then
echo -e "10.deploy ${tarDir} successfully at ${date}\n"
else
echo -e "deploy ${tarDir} fail at ${date}\n"
fi
使用支付宝打赏
使用微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏